//
//  EvaluateDetailViewController.m
//  Mahjong
//
//  Created by Chen on 16/9/17.
//  Copyright © 2016年 Elliot. All rights reserved.
//


#import "EvaluateDetailViewController.h"
#import "NoImageTableViewCell.h"
#import "ImageTableViewCell.h"
#import "AddShopCarView.h"
#import "EnsureListViewController.h"
#import "AppTools.h"
#import "EvaluateModel.h"

static NSString *identifierForImageTableViewCell = @"ImageTableViewCell";
static NSString *identifierForNoImageTableViewCell = @"NoImageTableViewCell";

@interface EvaluateDetailViewController ()<UITableViewDelegate,UITableViewDataSource,imageDelegate>
{
    UIView *grandView;
}
@property (nonatomic , strong) UIBarButtonItem *collectionItem;

@property (nonatomic , strong) UIView *buttomView;

@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dateArray;

@end

@implementation EvaluateDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"评价详情";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    [self createNaviBarItems];
    
    [self createTableView];
    
    [self createButtomView];
    self.dateArray = [NSMutableArray array];
    [self create];
}
-(void)create{
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=Getcomment&giftid=%ld&shopid=%ld",self.giftId,self.shopId] success:^(id result) {
        NSLog(@"%@",result);
        for (NSDictionary *dic in [result[0] objectForKey:@"date"]) {
            EvaluateModel *model = [[EvaluateModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dateArray addObject:model];
        }
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        
        
    }];
}
- (void)createNaviBarItems{
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    backItem.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = backItem;
    
    UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_share_pj"] style:(UIBarButtonItemStylePlain) target:self action:@selector(shareAction)];
    shareItem.tintColor = [UIColor grayColor];

    self.collectionItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_collect_pj"] style:(UIBarButtonItemStylePlain) target:self action:@selector(collectionAction)];
    self.collectionItem.tintColor = [UIColor grayColor];

    self.navigationItem.rightBarButtonItems = @[shareItem,self.collectionItem];
    
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)shareAction{
    // 这里需要继承分享的SDK 然后执行分享操作
}

- (void)collectionAction{
    // 收藏要注意 盘点收藏状态修改Item的图片
    // 这里已经设成了属性
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight - kTabbarHeight- kNavigationBarHeight) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:identifierForImageTableViewCell bundle:nil] forCellReuseIdentifier:identifierForImageTableViewCell];
    [self.tableView registerNib:[UINib nibWithNibName:identifierForNoImageTableViewCell bundle:nil] forCellReuseIdentifier:identifierForNoImageTableViewCell];
    [self.view addSubview:self.tableView];
}

#pragma mark - TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dateArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EvaluateModel *model = self.dateArray[indexPath.row];
    if (model.imagelist.count == 0) {
        NoImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForNoImageTableViewCell forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else {
        ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForImageTableViewCell     forIndexPath:indexPath];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(void)image{
    //创建一个黑色背景
    //初始化一个用来当做背景的View。我这里为了省时间计算，宽高直接用的5s的尺寸
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT)];
    grandView = bgView;
    [bgView setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:bgView];
    
    //创建显示图像的视图
    //初始化要显示的图片内容的imageView（这里位置继续偷懒...没有计算）
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, 200)];
    //要显示的图片，即要放大的图片
    [imgView setImage:[UIImage imageNamed:@"abc.jpg"]];
    [bgView addSubview:imgView];
    
    imgView.userInteractionEnabled = YES;
    //添加点击手势（即点击图片后退出全屏）
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView)];
    [imgView addGestureRecognizer:tapGesture];
    
    [self shakeToShow:bgView];//放大过程中的动画
}
-(void)closeView{
    [grandView removeFromSuperview];
}
//放大过程中出现的缓慢动画
- (void) shakeToShow:(UIView*)aView{
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.5;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [aView.layer addAnimation:animation forKey:nil];
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 160;
    } else {
        return 225;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        //AccountInfoViewController *accountInfoVC = [[AccountInfoViewController alloc] init];
        //[self.navigationController pushViewController:accountInfoVC animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //设置separatorInset(iOS7之后)
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    //设置layoutMargins(iOS8之后)
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //设置separatorInset(iOS7之后)
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    //设置layoutMargins(iOS8之后)
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)createButtomView{
    self.buttomView = [[UIView alloc] initWithFrame:CGRectMake(0, kUIScreenHeight - kTabbarHeight - kNavigationBarHeight, kUIScreenWidth, kTabbarHeight)];
    
    UIButton *service = [UIButton buttonWithType:(UIButtonTypeCustom)];
    service.frame = CGRectMake(15, 0, 30, kTabbarHeight - 15);
//    [service setTitle:@"客服" forState:(UIControlStateNormal)];
    [service setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [service setImage:[UIImage imageNamed:@"tab_but_custum"] forState:(UIControlStateNormal)];
    [service addTarget:self action:@selector(serviceAction:) forControlEvents:(UIControlEventTouchUpInside)];
//    [service layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyleTop) imageTitleSpace:0];
    service.titleLabel.font = [UIFont systemFontOfSize:13];
    service.titleLabel.textAlignment = NSTextAlignmentCenter;
    //[service setImageEdgeInsets:UIEdgeInsetsMake(-40, -40, -40, -40)];
    //[service setTitleEdgeInsets:UIEdgeInsetsMake(-30, -30, -30, -30)];
    [self.buttomView addSubview:service];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, kTabbarHeight-15, 40, 13)];
    label.text = @"客服";
    label.textAlignment = NSTextAlignmentCenter;
    [label setFont:[UIFont systemFontOfSize:13]];
    [self.buttomView addSubview:label];
    
    UIButton *shopCar = [UIButton buttonWithType:(UIButtonTypeCustom)];
    shopCar.frame = CGRectMake(90, 0, 30, kTabbarHeight - 15);
//    [shopCar setTitle:@"购物车" forState:(UIControlStateNormal)];
    [shopCar setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [shopCar setImage:[UIImage imageNamed:@"tab_but_shopcart"] forState:(UIControlStateNormal)];
    [shopCar addTarget:self action:@selector(shopCarAction:) forControlEvents:(UIControlEventTouchUpInside)];
//    [shopCar layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyleTop) imageTitleSpace:0];
    shopCar.titleLabel.font = [UIFont systemFontOfSize:13];
    shopCar.titleLabel.textAlignment = NSTextAlignmentCenter;
    //[shopCar setTitleEdgeInsets:UIEdgeInsetsMake(-20, -20, -20, -20)];
    //[shopCar setImageEdgeInsets:UIEdgeInsetsMake(-20, -20, -20, -20)];
    [self.buttomView addSubview:shopCar];
    UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(90, kTabbarHeight-15, 40, 13)];
    la.text = @"购物车";
    la.textAlignment = NSTextAlignmentCenter;
    [la setFont:[UIFont systemFontOfSize:13]];
    [self.buttomView addSubview:la];
    
    
    UIButton *buyBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    buyBtn.frame = CGRectMake(kUIScreenWidth - 100, 7.5, 80, kTabbarHeight - 15);
    [buyBtn setTitle:@"立刻购买" forState:(UIControlStateNormal)];
    [buyBtn setBackgroundColor:[UIColor redColor]];
    
    buyBtn.layer.cornerRadius = 5;
    buyBtn.clipsToBounds = YES;
    [self.buttomView addSubview:buyBtn];
    [buyBtn addTarget:self action:@selector(buyBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 1)];
    lineView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    [self.buttomView addSubview:lineView];
    
    [self.view addSubview:self.buttomView];
}

- (void)serviceAction:(UIButton *)btn{
    
}

- (void)shopCarAction:(UIButton *)btn{
    AddShopCarView *addShopCarView = [[AddShopCarView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight)];
    [self.view.window addSubview:addShopCarView];
}

- (void)buyBtnAction:(UIButton *)btn{
    EnsureListViewController *vc = [[EnsureListViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
